/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

var wcpKnowdialog;
var wcpKnowEditor;
KindEditor.plugin('wcpknow', function (K) {
    wcpKnowEditor = this, name = 'wcpknow';
    wcpKnowEditor.clickToolbar(name, function () {
        wcpKnowdialog = K
            .dialog({
                width: 250,
                title: '选择知识',
                body: '<div style="margin:10px;text-align: center;"><input id="wcpknow_input_id" type="text"/><span class="ke-button-common ke-button-outer ke-dialog-yes" title="查找"><input class="ke-button-common ke-button" id="wcpknow_button_id" value="查找" type="button"></span><br/><div class="wcpknow_search_rbox_c" id="wcpknow_search_rbox"></div></div>',
                closeBtn: {
                    name: '关闭',
                    click: function (e) {
                        wcpKnowdialog.remove();
                    }
                }
            });
        loadKnow_wcpKnow();
        $('#wcpknow_button_id').bind('click', function () {
            loadKnow_wcpKnow($('#wcpknow_input_id').val());
        });
        $('#wcpknow_input_id').keydown(function (e) {
            if (e.keyCode == 13) {
                loadKnow_wcpKnow($(
                    '#wcpknow_input_id')
                    .val());
            }
        });
    });
});
function loadKnow_wcpKnow(knowtitle) {
    $('#wcpknow_search_rbox').html('loading...');
    $.post('home/FPsearchKnow.do', {'knowtitle': knowtitle}, function (flag) {
        if (flag.size > 0) {
            $('#wcpknow_search_rbox').html('');
            $(flag.list).each(
                function (i, obj) {
                    $('#wcpknow_search_rbox').append(
                            '<a onClick="clickLink_wcpKnow(this)" id="'
                            + obj.ID + '" wcptype="'
                            + obj.DOMTYPE + '">' + obj.TITLE
                            + '</a></br>');
                });
        } else {
            alert('未匹配到相关知识!');
        }
    }, 'json');
}
function clickLink_wcpKnow(flag) {
    var id = $(flag).attr('id');
    var title = $(flag).text();
    var type = $(flag).attr('wcptype');
    wcpKnowEditor.insertHtml('<a href="webdoc/view/Pub' + id + '.html">' + title + '</a>');
    wcpKnowdialog.remove();
}


KindEditor.lang({ wcpknow: '插入知识' });